package xgenerator.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import xgenerator.converter.JavaTypeConverter;
import xgenerator.dao.DataSourceConfigDao;
import xgenerator.dao.MetadataDao;
import xgenerator.factory.DaoFactoryConfig;
import xgenerator.factory.DaoFactory;
import xgenerator.model.FieldMetadata;
import xgenerator.model.ModelMetadata;
import xgenerator.service.MetadataService;
import xgenerator.util.StringUtils;

/**
 * <p>
 * Title：MetadataServiceImpl
 * </p>
 * <p>
 * Description：
 * </p>
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * <p>
 * Company: liss
 * </p>
 * @author <a href="mailto:shushanlee@msn.com">liss</a>
 * @version 1.0
 * @see 
 * @since 1.0
 */
public class MetadataServiceImpl implements MetadataService {
	
	/**
	 * 初始化行数
	 */
	public static final int INIT_ROWS_COUNT = 5;
	
	/**
	 * 数据源配置Dao
	 */
	private DataSourceConfigDao dataSourceConfigDao;
	
	/**
	 * constructor
	 */
	public MetadataServiceImpl() {
		this.dataSourceConfigDao = DaoFactory.getInstance(DaoFactoryConfig.XML_CLASS).getDataSourceConfigDao();
	}

	/**
	 * <p>
	 * Title：获取配置的所有数据源列表
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @return
	 * @see xgenerator.service.MetadataService#getDataSourceNames()
	 */
	public Vector<String> getDataSourceNames() {
		return this.dataSourceConfigDao.findAllDataSourceNames();
	}

	/**
	 * <p>
	 * Title：根据数据源名称，获取数据库的所有表
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param dataSourceName
	 * @return
	 * @see xgenerator.service.MetadataService#getTables(java.lang.String)
	 */
	public Vector<String> getTables(String dataSourceName) {
		String dbType = this.dataSourceConfigDao.getDbType(dataSourceName);
		if(StringUtils.isBlank(dbType)) {
			throw new RuntimeException("请选择数据源！");
		}
		MetadataDao metadataDao = DaoFactory.getInstance(dbType).getMetadataDao();
		
		return metadataDao.getTables(dataSourceName);
	}
	
	/**
	 * <p>
	 * Title：
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param dataSourceName
	 * @param tableName
	 * @return
	 * @see xgenerator.service.MetadataService#getTableColumns(java.lang.String, java.lang.String)
	 */
	public Vector<String> getTableColumns(String dataSourceName, String tableName) {
		String dbType = this.dataSourceConfigDao.getDbType(dataSourceName);
		if(StringUtils.isBlank(dbType)) {
			throw new RuntimeException("请选择数据源！");
		}
		MetadataDao metadataDao = DaoFactory.getInstance(dbType).getMetadataDao();
		
		return metadataDao.getTableColumns(dataSourceName, tableName);
	}

	/**
	 * <p>
	 * Title：ModelMetadata
	 * </p>
	 * @param dataSourceName
	 * @param tableName
	 * @return
	 * @see xgenerator.service.MetadataService#getModelMetadata(java.lang.String, java.lang.String)
	 */
	public ModelMetadata getModelMetadata(String dataSourceName, String tableName) {
		ModelMetadata modelMetadata = new ModelMetadata();
		modelMetadata.setMetadataModelName(tableName);
		
		String tableComments = null;
		List<FieldMetadata> fieldMetadatas = null;
		if (StringUtils.isBlank(dataSourceName) || StringUtils.isBlank(tableName)) {//初始界面数据
			tableComments = "";
			fieldMetadatas = new ArrayList<FieldMetadata>();
			for (int i = 0; i < INIT_ROWS_COUNT; i++) {
				FieldMetadata oneMetadata = new FieldMetadata();
				oneMetadata.setNullable(true);
				oneMetadata.setKeyField(false);
				fieldMetadatas.add(oneMetadata);
			}
		} else {
			String dbType = this.dataSourceConfigDao.getDbType(dataSourceName);
			if(StringUtils.isBlank(dbType)) {
				throw new RuntimeException("请选择数据源！");
			}
			MetadataDao metadataDao = DaoFactory.getInstance(dbType).getMetadataDao();
			metadataDao.setPropertyTypeConverter(new JavaTypeConverter());
			
			tableComments = metadataDao.getTableComments(dataSourceName, tableName);
			fieldMetadatas = metadataDao.getTableColumnMetadata(dataSourceName, tableName);
		}
		modelMetadata.setModelDescription(tableComments);
		modelMetadata.setFieldMetadatas(fieldMetadatas);

		return modelMetadata;
	}

	public DataSourceConfigDao getDatasourceDao() {
		return dataSourceConfigDao;
	}

	public void setDatasourceDao(DataSourceConfigDao dataSourceConfigDao) {
		this.dataSourceConfigDao = dataSourceConfigDao;
	}

}
